//
//  MealController.mm
//  Meal Wheel
//
//  Created by Justin Dell on 7/5/08.
//  Copyright 2008 . All rights reserved.
//

#import "MealController.h"
#import "FoodTree.h"
#import <vector>

// Global variables
int numFoods    = 0;
int numBread    = 0;
int numDairy    = 0;
int numMeat     = 0;
int numFruit    = 0;
int numVeggie   = 0;
int numSweet    = 0;


@implementation MealController

-(IBAction)add:(id)sender {
    NSString *foodName = [foodField stringValue];
    if ([foodName compare:@""] == NSOrderedSame) return;
    
    numFoods++;
    switch (numFoods) {
        case 1:
            [one setTitle:foodName];
            [one setEnabled:YES];
            break;
        case 2:
            [two setTitle:foodName];
            [two setEnabled:YES];
            break;
        case 3:
            [three setTitle:foodName];
            [three setEnabled:YES];
            break;
        case 4:
            [four setTitle:foodName];
            [four setEnabled:YES];
            break;
        case 5:
            [five setTitle:foodName];
            [five setEnabled:YES];
            break;
        case 6:
            [six setTitle:foodName];
            [six setEnabled:YES];
            break;
        default:
            numFoods--;
            return;
    }
    
    [self displayIcon];
    [foodField setStringValue:@""];
}

-(void)removeIcon:(string)type {
        if (!type.compare("Bread")) [self removeBread];
        if (!type.compare("Dairy")) [self removeDairy];
        if (!type.compare("Meat"))  [self removeMeat];
        if (!type.compare("Fruit")) [self removeFruit];
        if (!type.compare("Veggie"))[self removeVeggie];
        if (!type.compare("Sweet")) [self removeSweet];
}

-(IBAction)removeOne:(id)sender {
    
    // Remove the corresponding icons
    vector<string> types = [fetcher getFoodByName:[[one title] UTF8String]]->getTypes();
    for (int i = 0; i < types.size(); i++) {
        [self removeIcon:(types[i])];
    }
    
    // Remove the food from the meal
    if (numFoods == 1) {
        [one setTitle:@""];
        [one setEnabled:NO];
    }
    else {
        [self moveUp:1];
    }
    numFoods--;
}

-(IBAction)removeTwo:(id)sender {
    
    // Remove the corresponding icons
    vector<string> types = [fetcher getFoodByName:[[two title] UTF8String]]->getTypes();
    for (int i = 0; i < types.size(); i++) {
        [self removeIcon:(types[i])];
    }
    
    // Remove the food from the meal
    if (numFoods == 2) {
        [two setTitle:@""];
        [two setEnabled:NO];
    }
    else {
        [self moveUp:2];
    }
    numFoods--;
}

-(IBAction)removeThree:(id)sender {
    
    // Remove the corresponding icons
    vector<string> types = [fetcher getFoodByName:[[three title] UTF8String]]->getTypes();
    for (int i = 0; i < types.size(); i++) {
        [self removeIcon:(types[i])];
    }
    
    // Remove the food from the meal
    if (numFoods == 3) {
        [three setTitle:@""];
        [three setEnabled:NO];
    }
    else {
        [self moveUp:3];
    }
    numFoods--;
}

-(IBAction)removeFour:(id)sender {
    
    // Remove the corresponding icons
    vector<string> types = [fetcher getFoodByName:[[four title] UTF8String]]->getTypes();
    for (int i = 0; i < types.size(); i++) {
        [self removeIcon:(types[i])];
    }
    
    // Remove the food from the meal
    if (numFoods == 4) {
        [four setTitle:@""];
        [four setEnabled:NO];
    }
    else {
        [self moveUp:4];
    }
    numFoods--;
}

-(IBAction)removeFive:(id)sender {
    
    // Remove the corresponding icons
    vector<string> types = [fetcher getFoodByName:[[five title] UTF8String]]->getTypes();
    for (int i = 0; i < types.size(); i++) {
        [self removeIcon:(types[i])];
    }
    
    // Remove the food from the meal
    if (numFoods == 5) {
        [five setTitle:@""];
        [five setEnabled:NO];
    }
    else {
        [self moveUp:5];
    }
    numFoods--;
}

-(IBAction)removeSix:(id)sender {
    
    // Remove the corresponding icons
    vector<string> types = [fetcher getFoodByName:[[six title] UTF8String]]->getTypes();
    for (int i = 0; i < types.size(); i++) {
        [self removeIcon:(types[i])];
    }
    
    // Remove the food from the meal
    if (numFoods == 6) {
        [six setTitle:@""];
        [six setEnabled:NO];
    }
    else {
        [self moveUp:6];
    }
    numFoods--;
}

-(void)moveUp:(int)remover {
    switch (remover) {
        case 1:
            [one setTitle:[two title]];
            [self moveUp:2];
            break;
        case 2:
            if (numFoods > 2) {
                [two setTitle:[three title]];
                [self moveUp:3];
            }
            else {
                [two setTitle:@""];
                [two setEnabled:NO];
            }
            break;
        case 3:
            if (numFoods > 3) {
                [three setTitle:[four title]];
                [self moveUp:4];
            }
            else {
                [three setTitle:@""];
                [three setEnabled:NO];
            }
            break;
        case 4:
            if (numFoods > 4) {
                [four setTitle:[five title]];
                [self moveUp:5];
            }
            else {
                [four setTitle:@""];
                [four setEnabled:NO];
            }
            break;
        case 5:
            if (numFoods > 5) {
                [five setTitle:[six title]];
                [self moveUp:6];
            }
            else {
                [five setTitle:@""];
                [five setEnabled:NO];
            }
            break;
        case 6:
            [six setTitle:@""];
            [six setEnabled:NO];
            break;
        default:
            break;
    }
}

-(void)displayIcon {
    if (!fetcher)
        fetcher = [[FoodFetcher alloc]init];
    Food *food = [fetcher getFoodByName:[[foodField stringValue] UTF8String]];
    
    vector<string> types = food->getTypes();
    for (int i = 0; i < types.size(); i++) {
        if (!types[i].compare("Bread")) [self addBread];
        if (!types[i].compare("Dairy")) [self addDairy];
        if (!types[i].compare("Meat"))  [self addMeat];
        if (!types[i].compare("Fruit")) [self addFruit];
        if (!types[i].compare("Veggie"))[self addVeggie];
        if (!types[i].compare("Sweet")) [self addSweet];
    }
}

-(void)addBread {
    numBread++;
    switch (numBread) {
        case 1:
            [bread1 setHidden:NO];
            break;
        case 2:
            [bread2 setHidden:NO];
            break;
        case 3:
            [bread3 setHidden:NO];
            break;
        case 4:
            [bread4 setHidden:NO];
            break;
        case 5:
            [bread5 setHidden:NO];
            break;
        default:
            break;
    }
}

-(void)addDairy {
    numDairy++;
    switch (numDairy) {
        case 1:
            [dairy1 setHidden:NO];
            break;
        case 2:
            [dairy2 setHidden:NO];
            break;
        case 3:
            [dairy3 setHidden:NO];
            break;
        case 4:
            [dairy4 setHidden:NO];
            break;
        case 5:
            [dairy5 setHidden:NO];
            break;
        default:
            break;
    }
}

-(void)addMeat {
    numMeat++;
    switch (numMeat) {
        case 1:
            [meat1 setHidden:NO];
            break;
        case 2:
            [meat2 setHidden:NO];
            break;
        case 3:
            [meat3 setHidden:NO];
            break;
        case 4:
            [meat4 setHidden:NO];
            break;
        case 5:
            [meat5 setHidden:NO];
            break;
        default:
            break;
    }
}

-(void)addFruit {
    numFruit++;
    switch (numFruit) {
        case 1:
            [fruit1 setHidden:NO];
            break;
        case 2:
            [fruit2 setHidden:NO];
            break;
        case 3:
            [fruit3 setHidden:NO];
            break;
        case 4:
            [fruit4 setHidden:NO];
            break;
        case 5:
            [fruit5 setHidden:NO];
            break;
        default:
            break;
    }
}

-(void)addVeggie {
    numVeggie++;
    switch (numVeggie) {
        case 1:
            [veggie1 setHidden:NO];
            break;
        case 2:
            [veggie2 setHidden:NO];
            break;
        case 3:
            [veggie3 setHidden:NO];
            break;
        case 4:
            [veggie4 setHidden:NO];
            break;
        case 5:
            [veggie5 setHidden:NO];
            break;
        default:
            break;
    }
}

-(void)addSweet {
    numSweet++;
    switch (numSweet) {
        case 1:
            [sweet1 setHidden:NO];
            break;
        case 2:
            [sweet2 setHidden:NO];
            break;
        case 3:
            [sweet3 setHidden:NO];
            break;
        case 4:
            [sweet4 setHidden:NO];
            break;
        default:
            break;
    }
}

-(void)removeBread {
    switch (numBread) {
        case 1:
            [bread1 setHidden:YES];
            break;
        case 2:
            [bread2 setHidden:YES];
            break;
        case 3:
            [bread3 setHidden:YES];
            break;
        case 4:
            [bread4 setHidden:YES];
            break;
        case 5:
            [bread5 setHidden:YES];
            break;
        default:
            break;
    }
    numBread--;
}

-(void)removeDairy {
    switch (numDairy) {
        case 1:
            [dairy1 setHidden:YES];
            break;
        case 2:
            [dairy2 setHidden:YES];
            break;
        case 3:
            [dairy3 setHidden:YES];
            break;
        case 4:
            [dairy4 setHidden:YES];
            break;
        case 5:
            [dairy5 setHidden:YES];
            break;
        default:
            break;
    }
    numDairy--;
}

-(void)removeMeat {
    switch (numMeat) {
        case 1:
            [meat1 setHidden:YES];
            break;
        case 2:
            [meat2 setHidden:YES];
            break;
        case 3:
            [meat3 setHidden:YES];
            break;
        case 4:
            [meat4 setHidden:YES];
            break;
        case 5:
            [meat5 setHidden:YES];
            break;
        default:
            break;
    }
    numMeat--;
}

-(void)removeFruit {
    switch (numFruit) {
        case 1:
            [fruit1 setHidden:YES];
            break;
        case 2:
            [fruit2 setHidden:YES];
            break;
        case 3:
            [fruit3 setHidden:YES];
            break;
        case 4:
            [fruit4 setHidden:YES];
            break;
        case 5:
            [fruit5 setHidden:YES];
            break;
        default:
            break;
    }
    numFruit--;
}

-(void)removeVeggie {
    switch (numVeggie) {
        case 1:
            [veggie1 setHidden:YES];
            break;
        case 2:
            [veggie2 setHidden:YES];
            break;
        case 3:
            [veggie3 setHidden:YES];
            break;
        case 4:
            [veggie4 setHidden:YES];
            break;
        case 5:
            [veggie5 setHidden:YES];
            break;
        default:
            break;
    }
    numVeggie--;
}

-(void)removeSweet {
    switch (numSweet) {
        case 1:
            [sweet1 setHidden:YES];
            break;
        case 2:
            [sweet2 setHidden:YES];
            break;
        case 3:
            [sweet3 setHidden:YES];
            break;
        case 4:
            [sweet4 setHidden:YES];
            break;
        default:
            break;
    }
    numSweet--;
}

@end
